﻿using AutoMapper;
using ERPPublic.eneity;
using MediatR;
using MySqlX.XDevAPI;
using NPOI.OpenXmlFormats.Dml.Diagram;
using NPOI.OpenXmlFormats.Wordprocessing;
using PersonnelManagement.Application.Command.ClientCommand;
using PersonnelManagement.Domain.Dto;
using PersonnelManagement.Domain.PersonnelDomain;
using PersonnelManagement.Dtos;
using PersonnelManagement.Infrstructre.Interface;
using SqlSugar;
using Client = PersonnelManagement.Domain.PersonnelDomain.Client;

namespace PersonnelManagement.Application.CommandHandler.ClientCommandHandlers
{
    public class ClientShowCommandHandlers : IRequestHandler<ClientShowCommand, PageDto<ClientShowDto>>
    {
        IClientRepository mRep;
        IMapper mapper;
        IClientcategoryRepository CGRepo;
        IProvinceRepository PRepo;
        ICityRepository CRepo;
        ISqlSugarClient db;

        public ClientShowCommandHandlers(IClientRepository rep, IMapper mapper, IClientcategoryRepository CGRepo, IProvinceRepository PRepo, ICityRepository CRepo, ISqlSugarClient db)
        {
            mRep = rep;
            this.mapper = mapper;
            this.CGRepo = CGRepo;
            this.PRepo = PRepo;
            this.CRepo = CRepo;
            this.db = db;
        }


        public async Task<PageDto<ClientShowDto>> Handle(ClientShowCommand request, CancellationToken cancellationToken)
        {
            // 执行连接查询并选择所需的字段  
            var query = db.Queryable<Client, Clientcategory, Province, City>((client, clientcategory, province, city) =>
                new JoinQueryInfos(
                    JoinType.Inner, client.CId == clientcategory.CId,
                    JoinType.Inner, client.ProvinceId == province.ProvinceId,
                    JoinType.Inner, client.CityId == city.CityId
                ))
                .Select((client, clientcategory, province, city) => new ClientShowDto
                {
                    // 映射所需字段  
                    ClientId = client.ClientId,
                    ClientNumber=client.ClientNumber,
                    ClientName=client.ClientName,
                    ProvinceId=client.ProvinceId,
                    CityId=client.CityId,
                    CPrimaryContact=client.CPrimaryContact,
                    CPickingStatus=client.CPickingStatus,
                    CApplicant=client.CApplicant,
                    CRecipients=client.CRecipients,
                    CPickTime=client.CPickTime,
                    ProvinceName=province.ProvinceName,
                    CityName=city.CityName,
                    CIndustry=client.CIndustry,
                    CName=clientcategory.CName
                }).Where(client => client.IsDel == false);

            // 根据请求参数应用过滤  
            if (request.ClientName != null)
            {
                query = query.Where(client => client.ClientName.Contains(request.ClientName));
            }
            if (request.CRecipients != null)
            {
                query = query.Where(client => client.CRecipients.Contains(request.CRecipients));
            }
            if (request.STime != null|| request.ETime != null)
            {
                query = query.Where(client => client.CPickTime>=request.STime && client.CPickTime<=request.ETime);
            }
            if (request.CPickingStatus != null)
            {
                query = query.Where(client => client.CPickingStatus==request.CPickingStatus);
            }
            if (request.CApplicant != null)
            {
                query = query.Where(client => client.CApplicant.Contains(request.CApplicant));
            }
            if (request.CIndustry != null)
            {
                query = query.Where(client => client.CIndustry==request.CIndustry);
            }
            // 执行查询并获取总数  
            var totalCount = await query.CountAsync();

            // 应用分页  
            var vendorList = await query
                .OrderByDescending(client => client.ClientId)
                .Skip((request.pageIndex - 1) * request.pageSize)
                .Take(request.pageSize)
                .ToListAsync();

            // 准备结果  
            var page = new PageDto<ClientShowDto>
            {
                lcount = totalCount,
                pcount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize),
                list = vendorList
            };

            return page;
        }
    }
}
